<?php
declare (strict_types = 1);

namespace app\controller;


use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;

class Uimages extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        if(\think\facade\Request::isAjax()){
            $list = Db::name("news")->order("id DESC")->paginate()->toArray();
            foreach ($list['data'] as $k=>$v){
                $list["data"][$k]["create_time"] = date("Y-m-d H:i",$v["create_time"]);
            }
            if($list){
                return json(["code"=>0,"msg"=>"成功","data"=>$list["data"]]);
            }else{
                return json(["code"=>40000,"msg"=>"失败","data"=>""]);
            }
        }
        View::assign("title","我的动态");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        View::assign("title","发布图片");
        return \view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $d = $request->post();
        if(!$d["img"]){
            return  json(["code"=>4000,"msg"=>"请先上传图片"]);
        }
        $mod = new \app\common\model\Images();

        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);

        $res = $mod->save($d);
        if($res){
            return  json(["code"=>200,"msg"=>"添加成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $article = Db::name("article")->find($id);
        View::assign("article",$article);

        if($user = Session::get("index_user")){
            $uid = $user["id"];
            $order = Db::name('article_order')->where(['aid'=>$article['id'],'uid'=>$uid,'status'=>2])->find();
            if($order){

                View::assign('order',$order);
            }else{
                View::assign('order',0);
            }
        }else{

            View::assign('order',0);
        }

        // 上一篇文章和下一篇文章
        $pre_article = Db::name("article")->where("id","<",$id)->order("id DESC")->find();
        $next_article = Db::name("article")->where("id",">",$id)->order("id ASC")->find();

        View::assign('pre_article',$pre_article);
        View::assign('next_article',$next_article);

        return view('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = \app\common\model\News::destroy($id);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }


}
